//
//  VCHomeViewController.m
//  VCardPro
//
//  Created by Nguyen Thong Thai on 9/17/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "VCHomeViewController.h"
#import "VCEditCardImageViewController.h"
#import "VCEmailComposerViewController.h"

@implementation VCHomeViewController

@synthesize takePictureButton;
@synthesize loadPictureButton;
@synthesize cardHolderButton;
@synthesize settingButton;
@synthesize composeMailButton;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidUnload
{
    [self setTakePictureButton:nil];
    [self setLoadPictureButton:nil];
    [self setCardHolderButton:nil];
    [self setSettingButton:nil];
    [self setComposeMailButton:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Even Handlers

- (IBAction)handleLoadPictureButton:(id)sender {
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
        UIImagePickerController *imgPickerController = [[UIImagePickerController alloc] init];
        imgPickerController.delegate = self;
        imgPickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        [self presentViewController:imgPickerController animated:YES completion:nil];
    } else {
        // don't support
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Don't support" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alert show];
    }
}

- (IBAction)handleCardHolderButton:(id)sender {
    
}

- (IBAction)handleSettingButton:(id)sender {
}

- (IBAction)handleTakePictureButton:(id)sender {
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        UIImagePickerController *imgPickerController = [[UIImagePickerController alloc] init];
        imgPickerController.delegate = self;
        imgPickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
        [self presentViewController:imgPickerController animated:YES completion:nil];
    } else {
        // don't support
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Don't support" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alert show];
    }
}

#pragma mark - UIImagePickerControllerDelegate methods

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    selectedImg = [info objectForKey:UIImagePickerControllerOriginalImage];
    [picker dismissModalViewControllerAnimated:YES];
    [self performSegueWithIdentifier:@"EditCardImageVCSegue" sender:self];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker;
{
    [picker dismissModalViewControllerAnimated:YES];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"EditCardImageVCSegue"]) {
        VCEditCardImageViewController *editCardImageVC = (VCEditCardImageViewController *)segue.destinationViewController;
        [editCardImageVC setOriginalImg:selectedImg];
    } else if ([[segue identifier] isEqualToString:@"ComposeMailSegue"]) {
        VCEmailComposerViewController *emailComposerVC = (VCEmailComposerViewController *)segue.destinationViewController;
    }
}

@end
